/**
 * Created by oneminute on 7/23/14.
 */
var assert = require("assert");
var should = require("should");
var logger = require("../../lib/utils/logger");
var testutils = require("../testutils");
var lz = require("../../lib/utils/modules")();

describe("测试User模块。", function () {
    var uid01 = lz.Utils.getUUID("u_");
    var user01 = null;
    var userdesc01 = {
        id: uid01,
        name: "测试名称01",
        email: {
            email: "user_0001@lz.com",
            authcode: "1010"
        },
        password: "123456",
        address: "测试地址",
        lon: 10,
        lat: 10,
        phone: "18611118888",
        description: "测试描述",
        photo: "photo01",
        inbusiness: true,
        userpoint: 1000,
        level: 5,
        state: 1,
        commodities: [
            {
                id: lz.Utils.getUUID("c_"),
                uid: uid01,
                description: "测试商品01",
                price: 10.22
            },
            {
                id: lz.Utils.getUUID("c_"),
                uid: uid01,
                description: "测试商品02",
                price: 20.22
            }
        ],
        accounts: [
            {
                id: lz.Utils.getUUID("b_"),
                uid: uid01,
                account: "11111111",
                publisher: "qq"
            },
            {
                id: lz.Utils.getUUID("b_"),
                uid: uid01,
                account: "18611118888",
                publisher: "weixin"
            }
        ],
        demands: [
            {
                id: lz.Utils.getUUID("d_"),
                uid: uid01,
                description: "测试需求01"
            },
            {
                id: lz.Utils.getUUID("d_"),
                uid: uid01,
                description: "测试需求02"
            }
        ],
        gifts: [
            {
                id: lz.Utils.getUUID("g_"),
                uid: uid01,
                description: "测试赠品01"
            },
            {
                id: lz.Utils.getUUID("g_"),
                uid: uid01,
                description: "测试赠品02"
            }
        ],
        online: true
    };
    var user02 = null;

    it("正常创建User对象。", function (done) {
        user01 = new lz.User(userdesc01);
        user01.save(function (_err, _user) {
            testutils.check(done, function () {
                _user.id.should.equal(user01.id);
            });
        });
    });

    it("只加载User对象的属性值，不递归加载Persistent类型对象。", function (done) {
        user02 = new lz.User({ _id: user01._id });
        user02.load({loadAll: false}, function (_err, _user) {
            testutils.check(done, function () {
                _user.commodities[0].class.should.equal("Commodity");
            });
        });
    });

    it("修改User对象中非Persistent类型的属性并保存。", function (done) {
        user01.description = "修改后的属性";
        user01.online = false;
        user01.save({saveAll: false}, function (_err, _user) {
            lz.ArangoDB.getObjectById(user01._id, function (_err, _newuser) {
                testutils.check(done, function () {
                    _newuser.description.should.equal(user01.description);
                    _newuser.online.should.equal(user01.online);
                    _newuser.commodities[0].class.should.equal("Commodity");
                });
            });
        });
    });

    it("根据_id正常加载User对象。", function (done) {
        var tmpUser = new lz.User({_id: user01._id});
        tmpUser.load(function(_err, _user) {
            testutils.check(done, function () {
                _user.email.email.should.equal("user_0001@lz.com");
                assert.notEqual(_user.email._id, null);
                tmpUser.email._id.should.equal(_user.email._id);
            });
        });
    });

    it("根据_id正常删除User对象。", function (done) {
        var userForDel = new lz.User({_id: user01._id});
        userForDel.load(function() {
            lz.Logger.debug(userForDel);
            userForDel.delete(function (_err) {
                lz.ArangoDB.getObjectById(userForDel._id, function (_err, _user) {
                    testutils.check(done, function () {
                        assert.equal(_user, null);
                    })
                });
            });
        });
    });

    it("使用register方法正常注册一个User对象。", function (done) {
        user02 = new lz.User({
            email: {
                email: "register_001@lz.com",
                authcode: "2020"
            }
        });

        user02.register(function (_err, _user) {
            testutils.check(done, function () {
                assert.equal(_err, null);
                assert.notEqual(_user, null);
                lz.ArangoDB.getObjectById(user02._id, function (_err, _user) {
                    _user.email.email.should.equal(user02.email.email);
                });
            });
        });
    });

    it("使用register方法注册，使用已存在的email。", function (done) {
        var user03 = new lz.User({
            email: {
                email: user02.email.email,
                authcode: "3030"
            }
        });

        user03.register(function (_err, _user) {
            testutils.check(done, function () {
                assert.notEqual(_err, null);
                _err.code.should.equal(lz.Config.errors.REGISTER_EMAIL_EXISTED.code);
            })
        });
    });

    it("使用load方法及loadAll:false选项加载一个user对象，然后删除。", function (done) {
        var user04 = new lz.User(userdesc01);
        user04.save(function (_err, _user) {
            var user05 = new lz.User({_id: user04._id});
            user05.load({loadAll: false}, function (_err, _user) {
                user05.refresh(function() {
                    user05.delete(function (_err) {
                        lz.ArangoDB.getObjectById(user05.commodities[0].aid, function (_err, _commodity) {
                            testutils.check(done, function () {
                                assert.equal(_commodity, null);
                            });
                        });
                    });
                });
            });
        });
    });

    it("测试setOnline函数。", function (done) {
        var online = user02.online;
        user02.setOnline(!online, function(_err, _user) {
            testutils.check(done, function () {
                assert.equal(!online, _user.online);
            });
        });
    });

    after(function (done) {
        user02.refresh(function () {
            user02.delete(function () {
                done();
            });
        });

    });
});